package com.gy.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.aaa.entity.CareHistory;
import com.aaa.entity.CareOrder;
import com.aaa.entity.CareOrderItem;
import com.aaa.vo.Result;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.gy.mapper.CareHistoryMapper;
import com.gy.mapper.CareOrderItemMapper;
import com.gy.mapper.CareOrderMapper;
import com.gy.service.CareHistoryService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author GaoYa
 * @Date 2023/3/25 10:27
 * @Version 1.5
 */
@Service
public class CareHistoryServiceImpl implements CareHistoryService {

    @Resource
    private CareHistoryMapper careHistory;

    @Resource
    private CareOrderMapper orderDao;

    @Resource
    private CareOrderItemMapper careOrderItemDao;

    /**
     * 处方收费
     *
     * @param regId
     * @return
     */
    @Override
    public Result<Map<String, Object>> findByUserId(String regId) {
        //根据regId查询对应的医师和患者
        QueryWrapper<CareHistory> queryWrap = new QueryWrapper<>();
        queryWrap.eq("reg_id", regId);
        CareHistory careHistory1 = careHistory.selectOne(queryWrap);
        if (careHistory1 == null) {
            return new Result<Map<String, Object>>(5000, "查询数据为空", null);
        }
        //获取ch_id关联his_care_order表
        String chId = careHistory1.getChId();
        QueryWrapper<CareOrder> query = new QueryWrapper<>();
        query.eq("ch_id", chId);
        List<CareOrder> careOrders = orderDao.selectList(query);
        List<Map<String, Object>> list = new ArrayList<>();
        if(careOrders.size()>0) {
            for (CareOrder careOrder : careOrders) {
                Map<String, Object> map1 = BeanUtil.beanToMap(careOrder);
                QueryWrapper<CareOrderItem> queryWrapper = new QueryWrapper<>();
                queryWrapper.eq("co_id", careOrder.getCoId());
                queryWrapper.eq("status", 0);
                List<CareOrderItem> careOrderItem = careOrderItemDao.selectList(queryWrapper);
                //根据order表关联order_item表
                //计算出订单总金额
                if (careOrderItem.size() != 0) {
                    Double sum = careOrderItem.stream().
                            map(item -> item.getAmount().doubleValue()).reduce((a, b) -> a + b).get();
                    map1.put("careOrderItem", careOrderItem);
                    map1.put("sum", sum);
                    list.add(map1);
                }
            }
        }
        if(list.isEmpty()){
            return new Result<>(4002, "请输入未支付的订单ID", null);
        }else{
            //把数据存放在map中返回
            Map<String, Object> map = new HashMap<>();
            map.put("careHistory", careHistory1);
            map.put("list",list);
            return new Result<>(2000, "成功", map);
        }
    }

    /**
     * 处方退费
     */
    @Override
    public Result<Map<String, Object>> BackFeeUserId(String regId) {
        //根据regId查询对应的医师和患者
        QueryWrapper<CareHistory> queryWrap = new QueryWrapper<>();
        queryWrap.eq("reg_id", regId);
        CareHistory careHistory1 = careHistory.selectOne(queryWrap);
        if (careHistory1 == null) {
            return new Result<Map<String, Object>>(5000, "查询数据为空", null);
        }
        //获取ch_id关联his_care_order表
        String chId = careHistory1.getChId();
        QueryWrapper<CareOrder> query = new QueryWrapper<>();
        query.eq("ch_id", chId);
        List<CareOrder> careOrders = orderDao.selectList(query);
        List<Map<String, Object>> list = new ArrayList<>();
        if (careOrders.size() > 0) {
            for (CareOrder careOrder : careOrders) {
                //转换为map格式
                Map<String, Object> map1 = BeanUtil.beanToMap(careOrder);
                QueryWrapper<CareOrderItem> queryWrapper = new QueryWrapper<>();
                queryWrapper.eq("co_id", careOrder.getCoId());
                queryWrapper.eq("status", 1);
                List<CareOrderItem> careOrderItem = careOrderItemDao.selectList(queryWrapper);
                //根据order表关联order_item表
                //计算出订单总金额
                if (careOrderItem.size() != 0) {
                    Double sum = careOrderItem.stream().
                            map(item -> item.getAmount().doubleValue()).reduce((a, b) -> a + b).get();
                    map1.put("careOrderItem", careOrderItem);
                    map1.put("sum", sum);
                    list.add(map1);
                }
            }
        }
        if (list.isEmpty()) {
            return new Result<>(4002, "请输入已支付并且未退费的名单的订单ID", null);
        } else {
            //把数据存放在map中返回
            Map<String, Object> map = new HashMap<>();
            map.put("careHistory", careHistory1);
            map.put("list", list);
            return new Result<>(2000, "成功", map);
        }
    }

    /**
     * 处方发药
     * @param regId
     * @return
     */
    @Override
    public Result<Map<String, Object>> BackFeeItemUserId(String regId) {
        QueryWrapper<CareHistory> queryWrap = new QueryWrapper<>();
        queryWrap.eq("reg_id", regId);
        CareHistory careHistory1 = careHistory.selectOne(queryWrap);
        if (careHistory1 == null) {
            return new Result<Map<String, Object>>(5000, "查询数据为空", null);
        }
        //获取ch_id关联his_care_order表
        String chId = careHistory1.getChId();
        QueryWrapper<CareOrder> query = new QueryWrapper<>();
        query.eq("ch_id", chId);
        List<CareOrder> careOrders = orderDao.selectList(query);
        List<Map<String, Object>> list = new ArrayList<>();
        if (careOrders.size() > 0) {
            for (CareOrder careOrder : careOrders) {
                Map<String, Object> map1 = BeanUtil.beanToMap(careOrder);
                QueryWrapper<CareOrderItem> queryWrapper = new QueryWrapper<>();
                queryWrapper.eq("co_id", careOrder.getCoId());
                queryWrapper.eq("item_type",0);
                queryWrapper.eq("status", 1);
                List<CareOrderItem> careOrderItem = careOrderItemDao.selectList(queryWrapper);
                //根据order表关联order_item表
                //计算出订单总金额
                if (careOrderItem.size() != 0) {
                    Double sum = careOrderItem.stream().
                            map(item -> item.getAmount().doubleValue()).reduce((a, b) -> a + b).get();
                    map1.put("careOrderItem", careOrderItem);
                    map1.put("sum", sum);
                    list.add(map1);
                }
            }
        }
        if (list.isEmpty()) {
            return new Result<>(4002, "请输入已经支付并且包含药品的订单号", null);
        } else {
            //把数据存放在map中返回
            Map<String, Object> map = new HashMap<>();
            map.put("careHistory", careHistory1);
            map.put("list", list);
            return new Result<>(2000, "成功", map);
        }
    }
}
